// pages/ucenter/my-withdrawal/my-withdrawal.js
import * as API_Distribution from '../../api/distribution'
import {
  Foundation,
  RegExp
} from '../../ui-utils/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /** 申请表单 */
    applyWithdrawalsForm: {
      /** 可提现金额 */
      can_rebate: 0,

      /** 提现金额 */
      apply_money: "",

      bank_card: "",

      /** 备注 */
      remark: ""
    },
    //余额
    can_rebate: '',
    //选择银行弹窗
    showActionsheet: false,
    //选择好的银行
    chooseBank: {},
    //银行卡列表
    bankCard: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    API_Distribution.getWithdrawalsCanRebate().then(response => {
      this.setData({
        'applyWithdrawalsForm.can_rebate': response.message,
        can_rebate: response.message
      })
    });
    API_Distribution.getWithdrawalsParams().then(res => {
      let choose = {}
      if (res.length != 0) {
        choose = res[0]
      }
      this.setData({
        bankCard: res,
        chooseBank: choose
      })
    })
  },
  updateValue(e) {
    this.setData({
      "applyWithdrawalsForm.apply_money": e.detail.value
    })

  },
  // 提现
  async handleApplyWithdrawals() {
    if (!this.data.applyWithdrawalsForm.apply_money) {
      wx.showToast({
        title: '提现金额不能为空或0！',
        icon: 'none'
      })
      return;
    } else if (!RegExp.money.test(this.data.applyWithdrawalsForm.apply_money)) {
      wx.showToast({
        title: '请输入正整数或者两位小数！',
        icon: 'none'
      })
      return;
    } else if (
      parseFloat(this.data.applyWithdrawalsForm.apply_money) >
      parseFloat(this.data.applyWithdrawalsForm.can_rebate)
    ) {
      wx.showToast({
        title: '已超可提现金额！',
        icon: 'none'
      })
      return;
    }
    if (this.data.bankCard.length == 0) {
      wx.showModal({
        title: "提示",
        showCancel: true,
        confirmColor: "#f42424",
        confirmText: "去设置",
        content: "您还没有设置提现银行卡",
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: "/ucenter/distribution/withdrawals-setting/withdrawals-setting"
            });
          }
        }
      });
      return;
    }
    const _params = {
      apply_money: this.data.applyWithdrawalsForm.apply_money,
      bank_card: this.data.chooseBank.bank_card,
      remark: this.data.applyWithdrawalsForm.remark
    };
    API_Distribution.applyWithdrawals(_params).then(() => {
      wx.showToast({
        title: '已提交申请'
      })
      setTimeout(() => {
        wx.navigateBack({
          delta: 1
        })
      }, 1000);
    });
  },
  //全部提现
  allWithdraw() {
    this.setData({
      'applyWithdrawalsForm.apply_money': this.data.can_rebate
    })
  },
  // 银行卡列表
  choose() {
    this.setData({
      showActionsheet: true
    })
  },
  //关闭弹窗
  close() {
    this.setData({
      showActionsheet: false
    })
  },
  //选择银行卡
  btnClickBack(event) {
    console.log(event)
    this.setData({
      'chooseBank.bank_name': event.currentTarget.dataset.name,
      'chooseBank.bank_card':event.currentTarget.dataset.cardno,
      showActionsheet: false
    })
  },
  //添加银行卡
  addNewCard() {
    wx.navigateTo({
      url: '/ucenter/distribution/withdrawals-setting/withdrawals-setting'
    })
    this.setData({
      showActionsheet: false
    })
  },
  //提现记录
  applyHistory() {
    wx.navigateTo({
      url: '/ucenter/distribution/withdrawals-history/withdrawals-history'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})